﻿@inherits SsoSectionComponentBase
@inject EnvironmentDataService EnvironmentData
@implements IDisposable

<MTextField @bind-Value="Value.Email" Label="@T("Email")" Placeholder="@T("EmailPlaceholder")"
            Outlined PrependInnerIcon="mdi-email" Rules="_emailRules" @ref="_emailTextField">
</MTextField>
<MTextField Class="mt-3" @bind-Value="Value.EmailCode" Outlined Label="@T("EmailCode")"
            Placeholder="@T("EmailCodePlaceholder")" Type="number"
            NumberProps="@(prop=> { prop.HideControl = true; })">
    <AppendContent>
        <MButton Style="margin-top:-16px;margin-right:-12px;height:54px;min-width: 120px;"
                 Class="primary rounded-r-lg" Disabled=!_canEmailCode OnClick="GetEmailCode">
            @(_canEmailCode ? @T("GetEmailCode") : _emailCodeTime)
        </MButton>
    </AppendContent>
</MTextField>

@code {
    [Parameter]
    public RegisterInputModel Value { get; set; } = new();

    [Parameter]
    public EventCallback<RegisterInputModel> ValueChanged { get; set; }

    [Parameter]
    public bool Required { get; set; }

    bool _canEmailCode = true;
    int _emailCodeTime = LoginOptions.GetEmailCodeInterval;
    System.Timers.Timer? _emailTimer;
    MTextField<string> _emailTextField = null!;

    [Inject]
    public IAuthClient AuthClient { get; set; } = null!;

    private IList<Func<string, StringBoolean>> _emailRules = new List<Func<string, StringBoolean>>();

    protected override void OnInitialized()
    {
        if (_emailTimer == null)
        {
            _emailTimer = new(1000 * 1);
            _emailTimer.Elapsed += EmailTimer_Elapsed;
        }
        base.OnInitialized();
    }

    protected override Task OnParametersSetAsync()
    {
        if (!_emailRules.Any())
        {
            _emailRules.Add(value => Regex.Match(value, LoginOptions.EmailRegular).Success ? true : T("EmailFormatError"));
        }
        if (Required && _emailRules.Count == 1)
        {
            _emailRules.Add(value => !string.IsNullOrEmpty(value) ? true : T("EmailRequired"));
        }

        return base.OnParametersSetAsync();
    }

    private void EmailTimer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
    {
        _ = InvokeAsync(() =>
        {
            _emailCodeTime--;
            if (_emailCodeTime == 0)
            {
                _emailTimer?.Stop();
                _canEmailCode = true;
                _emailCodeTime = LoginOptions.GetEmailCodeInterval;
            }
            StateHasChanged();
        });
    }

    private async Task GetEmailCode()
    {
        if (_emailTextField.Validate())
        {
            await AuthClient.UserService.SendEmailAsync(new SendEmailModel
            {
                Email = Value.Email,
                SendEmailType = SendEmailTypes.Register,
                Environment = EnvironmentData.Environment
            });

            await PopupService.EnqueueSnackbarAsync(T("The verification code is sent successfully"), AlertTypes.Success);
            _canEmailCode = false;
            _emailTimer?.Start();
        }
    }

    public void Dispose()
    {
        _emailTimer?.Dispose();
    }
}

